package com.tjhzzx.hnceCore2024.command;


import com.tjhzzx.hnceCore2024.servlet.AbstractServletCommand;
import com.tjhzzx.hnceCore2024.servlet.JSONResult;
import com.tjhzzx.hnceCore2024.user.UserManager;
import com.tjhzzx.hnceCore2024.user.entities.UserInfoItem;
import com.tridium.json.JSONObject;

import javax.baja.role.BIRoleService;
import javax.baja.sys.Sys;
import javax.baja.web.WebOp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class UserManageCommand extends AbstractServletCommand {

    private static final Logger LOGGER = Logger.getLogger(UserManageCommand.class.getName());

    public void postUserData(WebOp op, String queryString) {
        JSONResult pageResult = new JSONResult();
        Map<String, Object> params = resolveParams(queryString);
        List<UserInfoItem> items = UserManager.listUsers(op.getUser());
        long pageNo = Long.parseLong((String) params.get("pageNo"));
        long pageSize = Long.parseLong((String) params.get("pageSize"));
        long total = items!=null?items.size():0;
        List<JSONObject> list = new ArrayList<>();
        if (items != null && items.size() > 0) {
//            long totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
            long index = (pageNo - 1) * pageSize;
            for (long i = index; i < index + pageSize && i < items.size(); i++) {
                UserInfoItem item = items.get((int) i);
                JSONObject row = new JSONObject();
                row.put("id", i+1);
                row.put("username", item.name);
                row.put("role", item.role);
                row.put("email", item.email);
                row.put("phone", item.phone);
                list.add(row);
            }
        }
        pageResult.add("total", total);
        pageResult.add("list", list);
        pageResult.setState(0);
        pageResult.setMessage("查询成功");
        jsonResultResponse(pageResult);
    }

    public void postAddUser(WebOp op, String queryString) {
        Map<String, Object> params = resolveParams(queryString);
        UserInfoItem userInfoItem = new UserInfoItem();
        userInfoItem.name = (String) params.get("username");
        userInfoItem.password = (String) params.get("password");
        userInfoItem.phone = params.get("phone")!=null?(String)params.get("phone"):"";
        userInfoItem.email = params.get("email")!=null?(String)params.get("email"):"";
        userInfoItem.role = (String) params.get("role");
        boolean result = UserManager.addUser(op.getUser(), userInfoItem);
        if(result) {
            jsonResultResponse(JSONResult.success("添加用户成功"));
        } else {
            jsonResultResponse(JSONResult.error("添加用户失败"));
        }
    }

    public void postEditUser(WebOp op, String queryString) {
        Map<String, Object> params = resolveParams(queryString);
        UserInfoItem userInfoItem = new UserInfoItem();
        userInfoItem.name = (String) params.get("username");
//        userInfoItem.password = (String) params.get("password");
        boolean edit = false;
        if(params.get("phone")!=null) {
            userInfoItem.phone = (String)params.get("phone");
            edit = true;
        }
        if(params.get("email")!=null) {
            userInfoItem.email = (String)params.get("email");
            edit = true;
        }
//        if(params.get("role")!=null) {
//            userInfoItem.role = (String)params.get("role");
//            edit = true;
//        }
        if(edit) {
            boolean result = UserManager.editUser(op.getUser(), userInfoItem);
            if(result) {
                jsonResultResponse(JSONResult.success("修改用户成功"));
            } else {
                jsonResultResponse(JSONResult.error("修改用户失败"));
            }
        } else {
            jsonResultResponse(JSONResult.success("没有修改任何信息"));
        }

    }

    public void getUser(WebOp op, String queryString) {
        Map<String, Object> params = resolveParams(queryString);
        UserInfoItem userInfoItem = new UserInfoItem();
        userInfoItem.name = (String) params.get("username");
        BIRoleService roleService = (BIRoleService) Sys.getService(BIRoleService.TYPE);
        UserInfoItem item = UserManager.getUser(op.getUser(), userInfoItem);
        if(item != null) {
            jsonResultResponse(JSONResult.success("获取用户数据成功").add("user", item).add("roles", roleService.getRoleIds()));
        } else {
            jsonResultResponse(JSONResult.error("获取用户数据失败"));
        }
    }

    public void getRemoveUser(WebOp op, String queryString) {
        Map<String, Object> params = resolveParams(queryString);
        UserInfoItem userInfoItem = new UserInfoItem();
        userInfoItem.name = (String) params.get("username");
        boolean result = UserManager.removeUser(op.getUser(), userInfoItem);
        if(result) {
            jsonResultResponse(JSONResult.success("删除用户成功"));
        } else {
            jsonResultResponse(JSONResult.error("删除用户失败"));
        }
    }

    public void getAllRoles(WebOp op, String queryString) {
        BIRoleService roleService = (BIRoleService) Sys.getService(BIRoleService.TYPE);
        jsonResultResponse(JSONResult.success("获取角色成功").add("roles", roleService.getRoleIds()));
    }

}
